package com.nanyang.tms.manager.company.impl;

import cn.hutool.core.util.ObjectUtil;
import com.nanyang.common.core.annotation.SystemLog;
import com.nanyang.common.core.annotation.SystemMessage;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.common.tool.utils.uuid.GlobalIdGeneratorUtil;
import com.nanyang.tms.common.contstant.TmsSerializeConstant;
import com.nanyang.tms.common.dict.TmsCommonEnum;
import com.nanyang.tms.common.dict.TmsExceptionEnum;
import com.nanyang.tms.common.exception.TmsManagerException;
import com.nanyang.tms.common.util.TmsExceptionUtil;
import com.nanyang.tms.dao.company.CompanyAdjustmentDao;
import com.nanyang.tms.dao.company.CompanyDao;
import com.nanyang.tms.dao.company.CompanyStoredDao;
import com.nanyang.tms.dao.company.CompanyTransactionRecordDao;
import com.nanyang.tms.domain.company.*;
import com.nanyang.tms.manager.company.CompanyAdjustmentManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Component("companyAdjustmentManager")
public class CompanyAdjustmentManagerImpl implements CompanyAdjustmentManager {
    @Autowired
    private CompanyAdjustmentDao companyAdjustmentDao;
    @Autowired
    private CompanyStoredDao companyStoredDao;
    @Autowired
    private CompanyTransactionRecordDao companyTransactionRecordDao;
    @Autowired
    private GlobalIdGeneratorUtil globalIdGeneratorUtil;
    @Autowired
    private CompanyDao companyDao;
    @Override
    public PageBean<CompanyAdjustmentVO> listCompanyAdjustment(CompanyAdjustmentQuery query) {
        try {
            if (query == null) {
                log.error(TmsCommonEnum.PARAM_IS_NULL.getCode() + TmsCommonEnum.PARAM_IS_NULL.getMessage());
                throw new TmsManagerException(TmsCommonEnum.PARAM_IS_NULL.getCode() ,TmsCommonEnum.PARAM_IS_NULL.getMessage());
            }
            Integer count = companyAdjustmentDao.countAdjustment(query);
            PageBean<CompanyAdjustmentVO> page = new PageTableBean<CompanyAdjustmentVO>(query.getPageIndex(), query.getPageSize());
            if (null != count && count != 0) {
                page.setTotalItem(count);
                int startRow = page.getStartRow();
                if (startRow > 0) {
                    startRow -= 1;
                }
                query.setStartRow(startRow);
                page.addAll(companyAdjustmentDao.listAdjustment(query));
            }
            return page;
        } catch (Exception e) {
            throw TmsExceptionUtil.wrapException(e);
        }
    }
    @SystemMessage(messageType=SystemMessage.MessageType.SERVICE,template="storedAdjustmentMsg")
    @SystemLog(module =SystemLog.Module.STORED,template="storedAdjustment")
    @Override
    @Transactional
    public void saveCompanyAdjustment(CompanyAdjustmentDO companyAdjustmentDO) {
        try {

            CompanyStoredDO companyStoredDO=new CompanyStoredDO();
            if(companyAdjustmentDO.getAdjustmentType()==0){//增加
                companyStoredDO.setUseBalance(companyAdjustmentDO.getAmount());
            }else if(companyAdjustmentDO.getAdjustmentType()==1){//减少
                companyStoredDO.setUseBalance(companyAdjustmentDO.getAmount().negate());
            }
            companyStoredDO.setCompanyCode(companyAdjustmentDO.getCompanyCode());
            companyStoredDO.setGmtModifiedPin(companyAdjustmentDO.getGmtCreatePin());
            if(companyStoredDao.updateBalanceAmount(companyStoredDO)<=0){//修改客户储值余额
                throw new TmsManagerException(TmsExceptionEnum.ADJUSTMENT_AMOUNT_ERROR.getCode(), TmsExceptionEnum.ADJUSTMENT_AMOUNT_ERROR.getMessage());
            }
            CompanyStoredVO companyStoredVO = companyStoredDao.selectByCode(companyAdjustmentDO.getCompanyCode());
            if(ObjectUtil.isNull(companyStoredVO)){
                log.error(TmsExceptionEnum.COMPANY_STORE_FAIL.getCode() + TmsExceptionEnum.COMPANY_STORE_FAIL.getMessage());
                throw new TmsManagerException(TmsExceptionEnum.COMPANY_STORE_IS_NOT_EXIST.getCode(), TmsExceptionEnum.COMPANY_STORE_IS_NOT_EXIST.getMessage());
            }
            String companyName = companyDao.selectByCode(companyAdjustmentDO.getCompanyCode()).getCompanyName();
            String adjustCode=TmsSerializeConstant.COMPANYADJUSTMENT_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.COMPANYADJUSTMENT_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
            companyAdjustmentDO.setAdjustmentCode(adjustCode);
            companyAdjustmentDO.setCardNo(companyStoredVO.getCardNo());
            companyAdjustmentDO.setPin(companyStoredVO.getPin());
            companyAdjustmentDO.setUserName(companyStoredVO.getUserName());
            companyAdjustmentDO.setCompanyName(companyName);
            companyAdjustmentDao.insert(companyAdjustmentDO);//添加调账记录

            CompanyTransactionRecordDO companyTransactionRecordDO=new CompanyTransactionRecordDO();
            String recordCode=TmsSerializeConstant.COMPANYADTRANSACTION_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.COMPANYADTRANSACTION_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
            companyTransactionRecordDO.setRecordCode(recordCode);
            companyTransactionRecordDO.setBillTypeCode("ADJUSTMENT");
            companyTransactionRecordDO.setBillTypeName("调账");
            companyTransactionRecordDO.setBillCode(adjustCode);
            companyTransactionRecordDO.setCardNo(companyStoredVO.getCardNo());
            companyTransactionRecordDO.setCardName(companyStoredVO.getCardName());
            if(companyAdjustmentDO.getAdjustmentType()==0){//增加
                companyTransactionRecordDO.setAmountType(0);
                companyTransactionRecordDO.setAmount(companyAdjustmentDO.getAmount());
            }else if(companyAdjustmentDO.getAdjustmentType()==1){//减少
                companyTransactionRecordDO.setAmountType(1);
                companyTransactionRecordDO.setAmount(companyAdjustmentDO.getAmount());
            }
            companyTransactionRecordDO.setActualBalance(companyStoredVO.getActualBalance());
            companyTransactionRecordDO.setUseBalance(companyStoredVO.getUseBalance());
            companyTransactionRecordDO.setFrozenAmount(companyStoredVO.getFrozenAmount());
            companyTransactionRecordDO.setPin(companyStoredVO.getPin());
            companyTransactionRecordDO.setUserName(companyStoredVO.getUserName());
            companyTransactionRecordDO.setCompanyCode(companyStoredVO.getCompanyCode());
            companyTransactionRecordDO.setCompanyName(companyName);
            companyTransactionRecordDO.setIp(companyStoredVO.getIp());
            companyTransactionRecordDO.setGmtCreatePin(companyStoredVO.getPin());
            companyTransactionRecordDao.insert(companyTransactionRecordDO);//添加交易明细

        } catch (TmsManagerException e) {
            throw TmsExceptionUtil.wrapException(e);
        }
    }

}
